﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.Mvc;

namespace Guline.Hoster.Controllers
{
    public class MailAccountController : BaseController
    {
        //
        // GET: /MailAccount/

        public ActionResult Index()
        {
            return View(db.MailAccounts.ToList());
        }
        public ActionResult Create()
        {
            ViewBag.OrganizeID = new SelectList(this.db.Organizes, "ID", "Name");
            return View();
        }
        [HttpPost]
        public ActionResult Create(Guline.HosterModels.Object.MailAccount model)
        {
            ViewBag.OrganizeID = new SelectList(this.db.Organizes, "ID", "Name");
            if (ModelState.IsValid)
            {
                this.db.MailAccounts.Add(model);
                this.db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View();
        }
        public ActionResult Edit(int ID)
        {
            var MailAccount = db.MailAccounts.Where(m => m.ID == ID).FirstOrDefault();
            ViewBag.OrganizeID = new SelectList(this.db.Organizes, "ID", "Name", selectedValue: MailAccount.OrganizeID);
            return View(MailAccount);
        }
        [HttpPost]
        public ActionResult Edit(Guline.HosterModels.Object.MailAccount model)
        {
            ViewBag.OrganizeID = new SelectList(this.db.Organizes, "ID", "Name", selectedValue: model.OrganizeID);
            if (ModelState.IsValid)
            {
                db.Entry(model).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View();
        }
        public ActionResult Delete(int ID)
        {
            var MailAccount = db.MailAccounts.Where(m => m.ID == ID).FirstOrDefault();
            db.MailAccounts.Remove(MailAccount);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        public ActionResult editStatus(int ID)
        {
            var MailAccount = db.MailAccounts.Where(m => m.ID == ID).FirstOrDefault();
            if (MailAccount.Status == 0)
            {
                MailAccount.Status = 1;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                MailAccount.Status = 0;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
        }

    }
}
